/**************************************************************************
  This is an example for our Monochrome OLEDs based on SSD1306 drivers

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/category/63_98

  This example is for a 128x64 pixel display using I2C to communicate
  3 pins are required to interface (two I2C and one reset).

  Adafruit invests time and resources providing this open
  source code, please support Adafruit and open-source
  hardware by purchasing products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries,
  with contributions from the open source community.
  BSD license, check license.txt for more information
  All text above, and the splash screen below must be
  included in any redistribution.
 **************************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1// Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


void setup() {
  Serial.begin(115200);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  // delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  /*
    display.drawPixel(10, 10, SSD1306_WHITE);
    display.drawPixel(10, 20, 1);
    display.drawPixel(10, 30, 1);
    display.drawPixel(10, 40, 1);
    display.drawPixel(20, 10, 1);
    display.drawPixel(30, 10, 1);
    display.drawPixel(40, 10, 1);
  */
  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  //display.display();
  //delay(2000);
  // display.display() is NOT necessary after every single drawing command,
  // unless that's what you want...rather, you can batch up a bunch of
  // drawing operations and then update the screen all at once by calling
  // display.display(). These examples demonstrate both approaches...


}

char end_point =  ',';  //行结束
char start_char = 'S'; //开始接受字符
char end_char = 'E';  //结束字符

char buffer_data[128]; //每次读取字串128
int buffer_recv = 128; //缓存字数 128个


int loop_time = 0; //读到的次数 检测到S后 0-63
int nu_data = 0; //读到的位数0-127


void loop() {
  if (Serial.available() > 0) {
    nu_data = Serial.readBytesUntil(end_point , buffer_data, buffer_recv);
    Serial.print("read length:"); //显示读的长度
    Serial.println(nu_data);
    Serial.print("read bytes:"); //显示读的字节
    Serial.println(buffer_data);

    for (int x  = 0 ; x < nu_data; x++) {

      if ( buffer_data[x] == start_char ) { //判断开始
        Serial.print("Start"); //start
        loop_time = 0;
      }
      if ( buffer_data[x] == end_char ) { //判断结束
        Serial.print("end"); //end
        loop_time = 0;
        display.display();
        display.clearDisplay();
      }
      if  (nu_data == buffer_recv) { //判断是否读取到128位字节
        bool light = 0 ;
        if ( buffer_data[x] == '1') {
          light = 1;
        }
        if ( buffer_data[x] == '0') {
          light = 0;
        }
        display.drawPixel(x, loop_time ,  light);
        if (x == 127)  //判断字节128个
        {
          loop_time ++; //加行
        }
      }
    }

  }



  for (int i = 0 ; i < buffer_recv ; i++)
  {
    buffer_data[i] == '\0';
  }
  while (Serial.read() >= 0) {}
  delay(1);
  //delayMicroseconds(100);
}
